/**
 * @author jeroham
 * This is for common scripts and configuration calues
 */
/*
 *
 */
var currencySymbol = '$'; //universal currency symbol, may save on db
var target4Num;
var targetTab=0;
var currentDate = new Date();


function getFullName(name1, name2, name3){
    var full = name1;
    if (name2 != '') 
        full += ' ' + name2;
    if (name3 != '') 
        full += ' ' + name3;
    return full;
}

function StandardDate(date)
{
	var month = new String(new Number(date.getMonth())+1);
	if(month.length==1)
		month = '0'+month;
	var day = new String(date.getDate());
	if(day.length==1)
		day = '0'+day;
	return month+ '/'+day+'/'+date.getFullYear();	
}
function FormatDate(date)
{
	var fromdate = new Date(date);
	var month = new String(new Number(fromdate.getMonth())+1);
	if(month.length==1)
		month = '0'+month;
	var day = new String(fromdate.getDate());
	if(day.length==1)
		day = '0'+day;
	return fromdate.getFullYear()+'-'+month+ '-'+day;	
}

function formatNumber(num, currency){
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num)) 
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    var m = new Date();
    //m.toLocaleDateString
    num = Math.floor(num / 100).toString();
    if (cents < 10) 
        cents = "0" + cents;
    if (currency) {
        for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) 
            num = num.substring(0, num.length - (4 * i + 3)) + ',' +
            num.substring(num.length - (4 * i + 3));
        
        return (((sign) ? '' : '-') + '$' + num + '.' + cents);
    }
    else 
        return (((sign) ? '' : '-') + num + '.' + cents);
}

function formatCurrency(num){
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num)) 
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10) 
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) 
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
        num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}

//  End -->



function AddOne(control, amount){
    var amount1 = new Number(amount);
    var amount2 = new Number(document.all(control).value);
    var result = amount1 + amount2;
    
    document.all(control).value = result;
}

function AddUp(control, amount){
    if (amount == 'c') {
        control.value = '0.00';
        return;
    }
    var total = new Number(control.value);
    if (amount == 'd') {
        control.value = formatNumber(total * 100, false);
        return;
    }
    var namount = new Number(amount);
    total = (total * 10) + (amount / 100);
    control.value = formatNumber(total, false);
}
function ApplyNumPad(control)
{
	target4Num.value = control.value;
	ChangeTab(targetTab);
}

function ShowNumPad(control,tab)
{
	
	target4Num = control;
	targetTab = tab;
	var html = "<table border='1' class='numpad'>";
	
                html+= "<tr><td colspan='3' style='text-align:center;background:#4ce;'><input type='text' id='numdisplay' style='font:\"Impact\";width:70px;text-align:center;background:#4ce;border:none;font-size:10px;color:#333;' readonly value='0.00'/></td>";
                 html+= "</tr><tr><td><input type='button' class='numpad1' onclick='AddUp(document.all.numdisplay,7);' value='";
			 	 html+= "7' /></td><td><input type='button' class='numpad1' onclick='AddUp(document.all.numdisplay,8);' value='";
				 html+= "8' /></td><td><input type='button' class='numpad1' onclick='AddUp(document.all.numdisplay,9);' value='";
				 html+= "9' /></td></tr><tr><td><input type='button' class='numpad1' onclick='AddUp(document.all.numdisplay,4)' value='";
				 html+= "4' /></td><td><input type='button' class='numpad1' onclick='AddUp(document.all.numdisplay,5)' value='";
				 html+= "5' /></td><td><input type='button' class='numpad1' onclick='AddUp(document.all.numdisplay,6)' value='";
                 html+= "6' /></td></tr><tr><td><input type='button' class='numpad1' onclick='AddUp(document.all.numdisplay,1);' value='";
				 html+= "1' /></td><td><input type='button' class='numpad1' onclick='AddUp(document.all.numdisplay,2);' value='";
                 html+= "2' /></td><td><input type='button' class='numpad1' onclick='AddUp(document.all.numdisplay,3);' value='";
				 html+= "3' /></td></tr><tr><td><input type='button' class='numpad1' onclick='AddUp(document.all.numdisplay,0);' value='";
				 html+= "0' /></td><td><input type='button' class='numpad1' onclick='AddUp(document.all.numdisplay,\"d\");' value='00' /></td>";
                 html+= "<td><input type='button' class='numpad1' onclick='AddUp(document.all.numdisplay,\"c\")' value='C' /></td></tr><tr>";
				 html+="<td colspan='3'><input type='button' onclick='ChangeTab("+tab+");' style='font-size:9px;background:#9fa19a;' value='Cancel' />";	
                 html+= "&nbsp;&nbsp;<input type='button' onclick='ApplyNumPad(document.all.numdisplay);' style='font-size:9px;background:#9fa19a;text-align:center;width:60px;' value='OK'/></td></tr></table>";
                 
	document.all.numpad.innerHTML = html;
	document.all.numdisplay.value= control.value;	
	//alert(html);
	ChangeTab(7);// 7 reserved for numpad
}


function ShowDatePad(control, tab){

	target4Num = control;
	targetTab = tab;
	var html = "<table border='1' class='datepad'>";
	
	html += "<tr><td colspan='3' style='text-align:center;background:#4ce;'><input type='text' id='datedisplay' style='font:\"Impact\";width:70px;text-align:center;background:#4ce;border:none;font-size:10px;color:#333;' readonly value='0.00'/></td>";
	html += "</tr><tr><td>Month";
	html += "</td><td>Day";
	html += "</td><td>Year</td></tr><tr>";
	html += "<td><span id='monthspan'><span></td>";
	html += "<td><span id='dayspan'><span></td>";
	html += "<td><span id='yearspan'><span></td></tr><tr>";
	html += "<td colspan='3'><input type='button' onclick='ChangeTab(" + tab + ");' style='font-size:9px;background:#9fa19a;' value='Cancel' />";
	html += "&nbsp;&nbsp;<input type='button' onclick='ApplyNumPad(datedisplay);' style='font-size:9px;background:#9fa19a;text-align:center;width:60px;' value='OK'/></td></tr></table>";
	
	document.all.datepad.innerHTML = html;
	document.all.datedisplay.value = control.value;
	
	LoadYears();
	
	LoadMonths('es');
	//alert(html);
	ChangeTab(8);// 8 reserved for datepad
}


 function LoadYears(){
                // if (!google.gears.factory || !db) {
                //     alert('Error saving');
                //     return;
                // }
				
                var yearspan = document.all.yearspan;
				
                var html = "<select id='selyear'  onchange='LoadDays(document.all.selyear.value,this.value)' >"
                // var rs = db.execute("select distinct substr(name, length(name)-3) from sqlite_master where type='table' and  name like '%_20%'");
                
				var i = new Number(2007);
				
                var j = new Number(currentDate.getFullYear());
                
                //while (rs.isValidRow()) {
                for (i = 2007; i <= j; i++) {
                    //html += "<option value='" + rs.field(0) + "'";
                    html += "<option value='" + i + "'";
                    //if (rs.field(0) == new Date().getFullYear())
                    if (i == new Date().getFullYear()) 
                        html += " selected ";
                    //html += ">" + rs.field(0) + "</option>";
                    html += ">" + i + "</option>";
                    //rs.next();
                }
                //rs.close();
                html += "</select>";
                yearspan.innerHTML = html;
				
				var ddate = new Date(document.all.datedisplay.value);
				
				document.all.selyear.value =ddate.getFullYear();  
            }
			
function LoadMonths(option){
	
	var monthspan = document.all.monthspan;
	var html = '<select id="selmonth" onchange="LoadDays(this.value,document.all.selyear.value)" >';
	var months = ['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic'];
	if(option == 'en')
		months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
	for(var i=0;i<months.length;i++)
	{
		html+= "<option value='"+i+"'>"+ months[i]+"</option>";
	}
	html += "</select>";
	monthspan.innerHTML = html;
	var ddate = new Date(document.all.datedisplay.value);
	document.all.selmonth.value =ddate.getMonth();
	LoadDays(ddate.getMonth(),document.all.selyear.value);
}

function MonthName(option, month){
var months = ['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic'];
	if(option == 'en')
		months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
		return months[month];
		
}

function WeekDay(option){
	switch(option)
	{
		case 0: 
		return 'Dom';
		case 1: 
		return 'Lun';
		case 2: 
		return 'Mar';
		case 3: 
		return 'Mie';
		case 4: 
		return 'Jue';
		case 5: 
		return 'Vie';
		case 6: 
		return 'Sab';
		
	}
	return 0;
	
}

function LoadDays(month,year){
	
	var selmonth = new Number(month);
	var days = new Number(30);
	var dyear = new Number(year);

if(month==0 | month==2 | month==4 | month==6| month==7| month==9| month==11)
	days = 31;
if(month==1)
		{	if(dyear % 4 == 0)
				days = 29;
			else
				days = 28;
		}
		var dayspan = document.all.dayspan;
	
	var html = '<select id="selday" onchange="ChangeDate()">';
	for(i=1;i<=days;i++)
	{
		html+= "<option value='"+i+"'>"+ i +"</option>";	
	}
	html += "</select>";
	dayspan.innerHTML = html;
	var ddate = new Date(document.all.datedisplay.value);
	document.all.selday.value =ddate.getDate();
	ChangeDate();
}
function ChangeDate(){
	var ddate = document.all.datedisplay;
	var day = document.all.selday.value;
	var month = new Number(document.all.selmonth.value)+1;
	var year = document.all.selyear.value;
	ddate.value = month + '/' + day + '/' + year; 
}

/*
 * Returns a formated result line given certain variables
 * var1: id of the item
 * var2: description  1
 * var3: description  2
 * var4: description  3
 */
function SearchResult(var1, var2, var3, var4){
    return '<table class="resulttable"><tr><td colspan="2" class="pname">' + var2 +
    '<td style="width:40px"><a href="customerpie_bu.html?id=' +
    var1 +
    '">Load</a>' +
    '<tr><td style="width:90px">' +
    var3 +
    '<td style="width:90px">' +
    var4 +
    '<td style="width:40px"><a href="#" onclick="Delete(' +
    var1 +
    ')">Delete</a>' +
    '</table>';
}

function SearchResult4(var1, var2, var3, var4){
    return '<table class="resulttable"><tr><td colspan="2" class="pname">' + var2 +
    '<td style="width:40px"><a href="#" onclick="Load(' +
    var1 +
    ');">Load</a>' +
    '<tr><td style="width:90px">' +
    var3 +
    '<td style="width:90px">' +
    var4 +
    '<td style="width:40px"><a href="#" onclick="Delete(' +
    var1 +
    ')">Delete</a>' +
    '</table>';
}

function SearchResult3(var1, var2, var3, var4){
    return '<table class="resulttable"><tr><td colspan="2" class="pname">' + var2 +
    '<td style="width:40px"><a href="productpie.html?id=' +
    var1 +
    '">Load</a>' +
    '<tr><td style="width:90px">' +
    var3 +
    '<td style="width:90px">' +
    var4 +
    '<td style="width:40px"><a href="#" onclick="Delete(' +
    var1 +
    ')">Delete</a>' +
    '</table>';
}

function SearchResult2(var1, var2, var3, var4,var5){
    return '<table class="resulttable"><tr><td colspan="2" class="pname">' +
    var2 +
    '<td style="width:40px">' +
    var1 +
    '<tr><td style="width:90px">' +
    var3 +
    '<td style="width:90px">' +
    var4 +
    '<td style="width:40px">' +
    var5 +
    '</table>';
}

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to){
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};
